/**
 * Copyright 2018 orivil.com. All rights reserved.
 * Use of this source code is governed by a MIT-style
 * license that can be found at https://mit-license.org.
 */

package actions

import (
	"gitee.com/tugoer/orivil/mux"
	"gitee.com/tugoer/psy-testing/code/api/tags"
	"gitee.com/tugoer/psy-testing/code/model"
	"github.com/jinzhu/gorm"
	"net/http"
)


var TestHtml = []*mux.Action{
	{
		Name: "Testing Page",
		Method: "GET",
		Route: "/testing",
		Handler: func(ctx *mux.Context) {
			ctx.View("test")
		},
		ApiDoc:&mux.ApiDoc{
			Tags:[]string{tags.FrontHtml},
			Summary:"测试页面",
		},
	},
	{
		Name: "Index Page",
		Method: "GET",
		Route: "/",
		Handler: func(ctx *mux.Context) {
			id := ctx.Query().Get("id")
			if id != "" {
				sub, err := model.GetSubject(id)
				if err != nil {
					if err == gorm.ErrRecordNotFound {
						http.NotFound(ctx.Response, ctx.Request)
					} else {
						ctx.ErrWarning(err)
					}
				} else {
					ctx.Session().Set("redirect", "/testing?id="+id)
					ctx.View("qrcode").With("title", sub.Title)
				}
			} else if redirect := ctx.Session().Flash("redirect"); redirect != "" {
				ctx.Redirect(redirect)
			} else {
				ctx.View("index")
			}
		},
		ApiDoc:&mux.ApiDoc{
			Tags:[]string{tags.FrontHtml},
			Summary:"测试主页",
		},
	},
}

var adminDir = "admin"

var AdminHtml = []*mux.Action{
	{
		Name: "Admin Index Page",
		Method: "GET",
		Route: "/admin/advert",
		Handler: func(ctx *mux.Context) {
			ctx.VDir(adminDir).View("layout", "advert")
		},
		ApiDoc:&mux.ApiDoc{
			Tags:[]string{tags.AdminHtml},
			Summary:"广告管理页面",
		},
	},
	{
		Name: "Admin Spoof Page",
		Method: "GET",
		Route: "/admin/spoof",
		Handler: func(ctx *mux.Context) {
			ctx.VDir(adminDir).View("layout", "spoof")
		},
		ApiDoc:&mux.ApiDoc{
			Tags:[]string{tags.AdminHtml},
			Summary:"恶搞管理页面",
		},
	},
}